package com.fzrskj.cloud.free.core.tool;

import com.winiis.component.common.utils.bean.ClassUtils;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * CJsonTool.
 *
 * @author zj.
 *         Created on 2018/8/30 0030.
 */
public class CJsonTool extends JsonTool {
    /**
     * to convert json array string to JSONArray object.
     * @param js
     * @return
     */
    public static JSONArray getJsonArray(String js){
        if (StrTool.isBlankStr(js)) {
            return null;
        }

        try{
            JSONArray jsArray = JSONArray.fromObject(js);
            return jsArray;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取JsonArray.
     * @param json json string
     * @param key key.
     * @param defaultVal default value.
     * @return
     */
    public static JSONArray getJsonArray(String json, String key, JSONArray defaultVal) {
        if (StrTool.isBlankStr(json)) {
            return defaultVal;
        }

        JSONObject jsonObject = JSONObject.fromObject(json);
        if (null == jsonObject) {
            return defaultVal;
        }
        if (jsonObject.containsKey(key)) {
            return jsonObject.getJSONArray(key);
        }
        return defaultVal;
    }

    public static JSONObject getJSONObject(String json, String key,  JSONObject defaultVal) {
        if (StrTool.isBlankStr(json)) {
            return defaultVal;
        }

        JSONObject jsonObject = JSONObject.fromObject(json);
        if (null == jsonObject) {
            return defaultVal;
        }

        if (jsonObject.containsKey(key)) {
            return jsonObject.getJSONObject(key);
        }
        return defaultVal;
    }

    public static Map getMap(String json, String key) {
        return getMap(json, key, null);
    }

    public static Map getMap(String json, String key, Map defaultVal) {
        if (StrTool.isBlankStr(json)) {
            return defaultVal;
        }

        JSONObject jsonObject = JSONObject.fromObject(json);
        if (null == jsonObject) {
            return defaultVal;
        }

        if (jsonObject.containsKey(key)) {
            return (Map) jsonObject.get(key);
        }
        return defaultVal;
    }

    /**to get value from JSONArray
     * @param jsAry JSONArray object, you can call getJsonArray to get JSONArray
     * @param row row index
     * @param key key of item
     * @return null or object
     */
    public static Object getJsonArrayValue(JSONArray jsAry, int row, String key){
        Object ret = null;
        if ((jsAry==null) || (row>jsAry.size())) {
            return ret;
        }

        try{
            JSONObject obj = (JSONObject) jsAry.get(row);
            ret = obj.get(key);
        }catch(Exception e){
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 获取bean列表
     * @param p 请求数据json串.
     * @param key 字段.
     * @param defaultVale 默认值.
     * @param targetClass bean类.
     * @param <T> T.
     * @return bean列表.
     */
    public static <T> List<T> getListValue(String p, String key, List<T> defaultVale, Class<T> targetClass) {
        Object object = getValue(p, key);
        if (null == object) {
            return defaultVale;
        }
        List<Object> listObject = (List) object;
        List<T> result = new ArrayList<T>();
        for (Object obj : listObject) {
            T bean = ClassUtils.mapToClass((Map) obj, targetClass);
            result.add(bean);
        }
        return result;
    }

    /**add object item to json array
     * @param jsAry JSONArray
     * @param obj json item,such as "{"id":n}"
     * @return true if obj not exists in JSONArray and add successfully
     */
    public static boolean addObjToJsonArray(JSONArray jsAry, Object obj){
        boolean ret = false;
        try{
            if ((jsAry==null) || (obj==null)) {
                return false;
            }

            if (!objInJsonArray(jsAry, obj)){
                jsAry.add(obj);
                ret = true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return ret;
    }

    /**to judge object if in JSONArray
     * @param jsAry object of json array
     * @param obj item of json array item
     * @return true if exists or return false
     * @author lms 2016.08.09
     */
    public static boolean objInJsonArray(JSONArray jsAry, Object obj){
        if ((jsAry==null) || (obj==null))
            return false;

        return jsAry.contains(obj);
    }

    /**
     * get Double value of parameter.
     * @param p p.
     * @param key key.
     * @param defaultValue default value.
     * @return Double.
     */
    public static Double getDoubleVal(String p, String key, Double defaultValue) {
        String value = getStrValue(p, key, null);
        if (StrTool.isBlankStr(value)) {
            return defaultValue;
        }
        return Double.valueOf(value);
    }
}
